<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="add">+新增角色</el-button>
          <!--
            el-table
            data:绑定数据(数组数据类型)
           -->
          <el-table :data="list">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <!-- 绑定删除按钮 -->
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 10, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total,sizes,prev,pager,next,jumper"
              :total="page.total"
              @size-change="sizeChange"
              @current-change="currChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy>
          <!-- 动态组件结合异步组件使用 -->
          <component :is="'Info'" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <DialogItem
      ref="dialogItem"
      :is-show.sync="show"
      :list="list"
      @currChange="currChange"
      @getData="getData"
    />
  </div>
</template>

<script>
import { sysRole, sysRoleDel } from '@/api/setting.js'
import DialogItem from './components/dialogItem.vue'
export default {
  components: {
    DialogItem,
    Info: () => import('./components/info.vue') // 异步组件
  },
  data() {
    return {
      show: false,
      page: {
        page: 1,
        pagesize: 1,
        total: 100
      },
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      // 进入页面调用接口,获取数据并存储
      const res = await sysRole(this.page)
      this.page.total = res.data.total
      this.list = res.data.rows
      console.log('角色列表数据', res)
    },
    // 页码改变后的回调处理
    currChange(page) {
      this.page.page = page
      this.getData()
    },
    // 页容量改变后的回调处理
    sizeChange(size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    add() {
      this.show = true
    },
    // 删除
    del(row) {
      this.$confirm('请确定删除该角色吗?', '提示')
        .then(async() => {
          await sysRoleDel(row.id)
          this.$message.success('删除成功')
          this.currChange(1)
        })
        .catch(() => {})
    },
    edit(row) {
      /*
      编辑:1.打开弹框
      2.数据回显
      3.加入一个参数用于区分新增与编辑
      4.表单验证处理
      5.调用编辑接口
      6.调用完成关闭弹框,刷新数据,提示
      */
      this.show = true
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row))
      this.$refs.dialogItem.mode = 'edit'
    }
  }
}
</script>

<style lang="scss" scped>
.setting {
  padding: 15px;
  .pagination {
    text-align: right;
    padding-top: 15px;
  }
}
</style>
